package com.sw.leftrefreshrecyclerview;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;

import com.sw.leftrecyclerview.refresh.interfaces.OnLoadMoreListener;
import com.sw.leftrecyclerview.refresh.interfaces.OnRefreshListener;
import com.sw.leftrecyclerview.refresh.util.RecyclerViewStateUtils;
import com.sw.leftrecyclerview.refresh.view.LRecyclerViewAdapter;
import com.sw.leftrecyclerview.refresh.view.LeftRefreshRecyclerView;
import com.sw.leftrecyclerview.refresh.view.LoadingFooter;

public class MainActivity extends AppCompatActivity {

    private LeftRefreshRecyclerView lrv_main;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lrv_main = (LeftRefreshRecyclerView) findViewById(R.id.lrv_main);
        MainAdapter mainAdapter = new MainAdapter(this);
        lrv_main.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        lrv_main.setAdapter(new LRecyclerViewAdapter(mainAdapter));
        lrv_main.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        lrv_main.refreshComplete();
                    }
                }, 2000);
            }
        });

        lrv_main.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                LoadingFooter.State state = RecyclerViewStateUtils.getFooterViewState(lrv_main);
                if (state == LoadingFooter.State.Loading) {
                    return;
                }
                RecyclerViewStateUtils.setFooterViewState(MainActivity.this, lrv_main, 10, LoadingFooter.State.Loading, null);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        lrv_main.refreshComplete();
                        RecyclerViewStateUtils.setFooterViewState(MainActivity.this, lrv_main, 10, LoadingFooter.State.Normal, null);
                    }
                }, 2000);
            }
        });

    }
}
